//
//  AdviceFeedbackViewController.m
//  NOAHWM
//
//  Created by Frank on 5/30/13.
//  Copyright (c) 2013 Ryan. All rights reserved.
//

#import "AdviceFeedbackViewController.h"
#import "NYMemberCenterDataManager.h"
#import "NSString+MobileNum.h"

@interface AdviceFeedbackViewController ()
-(BOOL)isValidatePhone:(NSString *)phone;
@end

@implementation AdviceFeedbackViewController

@synthesize textFieldPhone;
@synthesize textView;
//@synthesize labelTips;
@synthesize labelWriteAdvice;

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setNaviTitle:@"意见反馈"];
    [self setLeftNaviItemWithTitle:nil imageName:@"Icon_Back"];
    [self setRightNaviItemWithTitle:nil imageName:nil];
//    self.labelTips.text = @"手机号";
    NSString *acountId = [NYMemberCenterDataManager sharedManager].loginMember.accountId;
    if (acountId !=nil) {
        NSString *phnum = [NYMemberCenterDataManager sharedManager].loginMember.phoneNum;
        if (phnum!=nil) {
            self.textFieldPhone.text = phnum;
        }
        else{
            self.textFieldPhone.text = nil;
        }
        
    }
    if ([self.textView.text isEqualToString:@""]) {
        self.labelWriteAdvice.hidden = NO;
    }
    else{
        self.labelWriteAdvice.hidden = YES;
    }
    //register notification for keyboard
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyBoardWillShow:)
                                                 name:UIKeyboardWillShowNotification
                                               object:nil];
    /*
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyBoardWillHide:)
                                                 name:UIKeyboardWillHideNotification
                                               object:nil];
     */
}

- (void)viewWillAppear:(BOOL)animated
{
    if ([self.textView.text isEqualToString:@""]) {
        self.labelWriteAdvice.hidden = NO;
    }
    else{
        self.labelWriteAdvice.hidden = YES;
    }

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    if(self.isViewLoaded && !self.view.window)
    {
        [[RYDownloaderManager sharedManager] cancelDownloaderWithDelegate:[NYMemberCenterDataManager sharedManager] purpose:kSendAdviceFeedbackKey];
        [[NSNotificationCenter defaultCenter] removeObserver:self];
        self.textView = nil;
        self.textFieldPhone = nil;
//        self.labelTips = nil;
        self.labelWriteAdvice = nil;
        
    }
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    
    [[RYDownloaderManager sharedManager] cancelDownloaderWithDelegate:[NYMemberCenterDataManager sharedManager] purpose:kSendAdviceFeedbackKey];
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    
    self.textView = nil;
//    self.labelTips = nil;
    self.textFieldPhone = nil;
    self.labelWriteAdvice = nil;
}

- (void)dealloc
{
    [[RYDownloaderManager sharedManager] cancelDownloaderWithDelegate:[NYMemberCenterDataManager sharedManager] purpose:kSendAdviceFeedbackKey];
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    
    [textView release];
//    [labelTips release];
    [textFieldPhone release];
    [labelWriteAdvice release];
    [super dealloc];
}

#pragma mark - BaseViewController methods
- (void)leftItemTapped
{
    [self.textView resignFirstResponder];
    [self.textFieldPhone resignFirstResponder];
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - action
- (IBAction)sendAdvice:(id)sender
{
    NSString *acountId = [NYMemberCenterDataManager sharedManager].loginMember.accountId;
    NSString *phoneNum = nil;
    NSString *message = nil;

    phoneNum = self.textFieldPhone.text;
            
    if ([NSString isMobileNumber:phoneNum]==NO) {
        message = [NSString stringWithFormat:[kRegisterPromptDict objectForKey:@"F000003E"],@"手机号"];
        if(phoneNum.length < 1)
            message = [NSString stringWithFormat:[kRegisterPromptDict objectForKey:@"F000001E"],@"手机号"];
        else if(phoneNum.length > 11)
            message = [NSString stringWithFormat:[kRegisterPromptDict objectForKey:@"F000002E"],@"手机号"];
        [[RYHUDManager sharedManager] showWithMessage:message customView:nil hideDelay:2.f];
        return;
    }
    
    if (self.textView.text.length < 1 ||self.textView.text.length > kMaxFeedbackLength) {
        if (self.textView.text.length < 1 ) {
            message = [NSString stringWithFormat:[kRegisterPromptDict objectForKey:@"F000001E"],@"意见反馈"];
        }
        else{
            message = [NSString stringWithFormat:[kRegisterPromptDict objectForKey:@"F000002E"],@"意见反馈"];
        }
        [[RYHUDManager sharedManager] showWithMessage:message customView:nil hideDelay:2.f];
        return;
    }

    NSString *url = [NSString stringWithFormat:@"%@%@",kServerAddress,[NSString stringWithFormat:kManageQuestionUrl,@"jsonp",acountId, self.textView.text, phoneNum]];
    url = [url stringByAppendingString:@"app000011"];
    url = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    [[RYDownloaderManager sharedManager] requestDataByGetWithURLString:url delegate:[NYMemberCenterDataManager sharedManager] purpose:kSendAdviceFeedbackKey];
        
    [[RYHUDManager sharedManager] startedNetWorkActivityWithText:@"发送中......"];
    [self.textFieldPhone resignFirstResponder];
}

#pragma mark - textview delegate
- (void)textViewDidChange:(UITextView *)tv
{
    if(tv.text.length > 0)
        labelWriteAdvice.hidden = YES;
    else
        labelWriteAdvice.hidden = NO;
}

#pragma mark - textfield delegate
- (BOOL)textFieldShouldReturn:(UITextField *)theTextField {
    if (theTextField == self.textFieldPhone) {
        [self.textFieldPhone resignFirstResponder];
    }
    return YES;
}


- (void)keyBoardWillShow:(NSNotification *)notification
{
    /*
    NSDictionary *info = notification.userInfo;
    CGSize keybSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
    
    CGRect rect = self.scrollView.frame;
    rect.size.height = self.view.frame.size.height-keybSize.height;
    self.scrollView.frame = rect;
    [self.scrollView setContentSize:kDefaultScrollSize];
     */
//    self.labelWriteAdvice.hidden = YES;

}

/*
- (void)keyBoardWillHide:(NSNotification *)notification
{
    CGRect rect = self.scrollView.frame;
    rect.size.height = self.view.frame.size.height;
    self.scrollView.frame = rect;
}
*/

#pragma mark - private methord
-(BOOL)isValidatePhone:(NSString *)phone
{
    NSString *Regex =@"(13[0-9]|14[57]|15[012356789]|18[02356789])\\d{8}";
    
    NSPredicate *mobileTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", Regex];
    
    return [mobileTest evaluateWithObject:phone];
}

@end
